package com.CST.controller.cb;

import com.CST.common.api.ResultObject;
import com.CST.dto.cb.CountInComeDTO;
import com.CST.dto.cb.DateDTO;
import com.CST.entity.cb.BdDepartment;
import com.CST.mapper.cst.cb.AmortizationDataMapper;
import com.CST.mapper.dataplatform.BdDepartmentMapper;
import com.CST.service.cb.IAmortizationDataService;
import com.CST.util.PageHelperTool;
import com.CST.vo.cb.ReportCostAccountVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/amortizationData")
@Api(tags = "分摊数据")
public class AmortizationDataController {
    
    @Autowired
    private IAmortizationDataService amortizationDataService;
    @Autowired
    private BdDepartmentMapper bdDepartmentMapper;
    @Autowired
    private AmortizationDataMapper  amortizationDataMapper;
    @PostMapping("/listAmortizationDataByDate")
    @ApiOperation("根据年月+院区获取分摊成本，临床类全成本报表，临床类全成本构成分析报表")
    public ResultObject listAmortizationDataByDate(@RequestBody DateDTO dateDTO){
        return amortizationDataService.listAmortizationDataByDate(dateDTO);
    }
    
    @PostMapping("/listAmortizationDataByPDeptCode/{pDeptCode}")
    @ApiOperation("根据年月+院区获取分摊成本+父级科室id获取分摊成本，临床类全成本报表，临床类全成本构成分析报表")
    public ResultObject listAmortizationDataByPDeptCode(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode){
        return amortizationDataService.listAmortizationDataByPDeptCode(dateDTO, pDeptCode);
    }

    @PostMapping("/exportExcel7")
    @ApiOperation("未分摊科室下载")
    public void exportExcel7(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        amortizationDataService.exportExcel7(response,dateDTO);
    }
    
    @PostMapping("/listCountInCome")
    @ApiOperation("收益总表数据")
    public ResultObject listCountInCome(@RequestBody CountInComeDTO countInComeDTO){
        if (!countInComeDTO.getStartYear().equals(countInComeDTO.getEndYear())){
            return ResultObject.failed("该页面不允许跨年查询！");
        }
        return amortizationDataService.listCountInCome(countInComeDTO);
    }
    
    @PostMapping("/listAmortizationDataWithAccount")
    @ApiOperation("医院各科室全程本表，会计分类汇总")
    public ResultObject listAmortizationDataWithAccount(@RequestBody DateDTO dateDTO){
        List<ReportCostAccountVO> list = amortizationDataService.listAmortizationDataWithAccount(dateDTO);
//        return ResultObject.successReturn(list);
        return ResultObject.successReturn(PageHelperTool.excutePageInfo(list, dateDTO.getPageNum(), dateDTO.getPageSize()));
    }

    /**
     * 科室成本分析
     * 数据整理和归档方式类似于医院各剋是全成本表（会计分类汇总）
     * 区别在于：不需要根据AccountCode group by
     * 需要：时间范围+父级科室
     * @param dateDTO
     * @param pDeptCode
     * @return
     */
    @PostMapping("/deptCostAnalysis/{pDeptCode}")
    @ApiOperation("科室成本分析")
    public ResultObject deptCostAnalysis(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode){

        LambdaQueryWrapper<BdDepartment> bdDepartmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bdDepartmentLambdaQueryWrapper.eq(BdDepartment::getDeptCode,pDeptCode);
        BdDepartment bdDepartment = bdDepartmentMapper.selectOne(bdDepartmentLambdaQueryWrapper);
        List<ReportCostAccountVO> list;
        if (bdDepartment.getParentCode().equals("1")){
           list = amortizationDataService.deptCostAnalysis(dateDTO, pDeptCode);
        }else {
            list = amortizationDataMapper.listAmortizationDataByDateAndPDeptWithDeptNew(dateDTO, null,pDeptCode);
        }

        return ResultObject.successReturn(list);
    }

    /**
     * 传入时间+科室
     * 获取科室成本的分析的详情
     * 根据account进行分组和计算
     * @return
     */
    @PostMapping("/listDeptCostAnalysis/{deptCode}")
    @ApiOperation("科室成本分析：科室成本会计分类详情分析")
    public ResultObject listDeptCostAnalysis(@RequestBody DateDTO dateDTO, @PathVariable String deptCode){
        List<ReportCostAccountVO> list = amortizationDataService.listDeptCostAnalysis(dateDTO, deptCode);
        return ResultObject.successReturn(list);
    }

    /**
     * 未分摊科室
     * @return
     */
    @PostMapping("/noAmortizationDept")
    @ApiOperation("未分摊科室")
    public ResultObject noAmortizationDept(@RequestBody DateDTO dateDTO){
        return ResultObject.successReturn(amortizationDataService.noAmortizationDept(dateDTO));
    }


    @PostMapping("/download2")
    public void exportExcel2(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        amortizationDataService.exportExcel2(response,dateDTO);
    }

    @PostMapping("/download3")
    public void exportExcel3(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        amortizationDataService.exportExcel3(response,dateDTO);
    }

    @PostMapping("/download4")
    public void exportExcel4(@RequestBody CountInComeDTO dateDTO, HttpServletResponse response) throws IOException {
        amortizationDataService.exportExcel4(response,dateDTO);
    }

    @PostMapping("/download6")
    public void exportExcel6(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        amortizationDataService.exportExcel6(response,dateDTO);
    }

}
